package com.easy;

import java.util.*;

/**
 * Created by Lxk on 2019/12/1.
 */
public class Solution1189 {

    private static Set<Character> characterSet = new HashSet<>();
    private static Map<Character, Integer> characterCounterMap = new HashMap<>();

    static {
        characterSet.add('a');
        characterSet.add('b');
        characterSet.add('l');
        characterSet.add('o');
        characterSet.add('n');

        characterCounterMap.put('a', 1);
        characterCounterMap.put('b', 1);
        characterCounterMap.put('l', 2);
        characterCounterMap.put('o', 2);
        characterCounterMap.put('n', 1);
    }

    //balloon
    public int maxNumberOfBalloons(String text) {
        Map<Character, Integer> counter = new HashMap<>();
        for (Character c : text.toCharArray()) {
            if (characterSet.contains(c)) {
                if (counter.keySet().contains(c)) {
                    counter.put(c, counter.get(c) + 1);
                } else {
                    counter.put(c, 1);
                }
            }
        }
        List<Integer> counterList = new ArrayList<>();
        for (Map.Entry<Character, Integer> entry : characterCounterMap.entrySet()) {
            if (counter.keySet().contains(entry.getKey())) {
                counterList.add(counter.get(entry.getKey()) / entry.getValue());
            } else {
                return 0;
            }
        }
        Optional<Integer> min = counterList.stream().min(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1 - o2;
            }
        });
        return min.isPresent() ? min.get() : 0;
    }
}
